package com.lgy.springboot_example.controller;

import com.alibaba.fastjson.JSONObject;
import com.lgy.springboot_example.entity.Administrator;
import com.lgy.springboot_example.entity.Role;
import com.lgy.springboot_example.request_validate.RequestPermission;
import com.lgy.springboot_example.request_validate.RequestPermissionEnum;
import com.lgy.springboot_example.utils.DataResult;
import com.lgy.springboot_example.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Api(tags = "注解控制请求权限测试")
@RequestMapping("validate")
@RestController
public class RequestValidateController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("adminLogin")
    @ApiOperation("模仿管理员登录存入用户权限信息到redis")
    public DataResult<Object> adminLogin() {
        Administrator admin = new Administrator();
        admin.setId(2);
        admin.setUserName("tom");
        Role role = new Role();
        role.setRoleName("child_admin");
        role.setUserModel(-1);
        role.setOrderModel(-1);
        role.setCommentModel(1);
        JSONObject loginInfo = new JSONObject();
        loginInfo.put("adminInfo", admin);
        loginInfo.put("role", role);
        redisTemplate.opsForValue().set(MyUtils.getToken(), loginInfo.toJSONString(), 30 * 60, TimeUnit.SECONDS);
        return new DataResult();
    }

    @PostMapping("getUserModelData")
    @ApiOperation("操作用户模块")
    @RequestPermission(RequestPermissionEnum.USER_MODEL)
    public DataResult<Object> getUserModelData() {
        return new DataResult("操作用户模块成功！");
    }

    @PostMapping("getOrderModelData")
    @ApiOperation("操作订单模块")
    @RequestPermission(RequestPermissionEnum.ORDER_MODEL)
    public DataResult<Object> getOrderModelData() {
        return new DataResult("操作订单模块成功！");
    }

    @PostMapping("getCommentModelData")
    @ApiOperation("操作评论模块")
    @RequestPermission(RequestPermissionEnum.COMMENT_MODEL)
    public DataResult<Object> getCommentModelData() {
        return new DataResult("操作评论模块成功！");
    }

    @PostMapping("getPublicModelData")
    @ApiOperation("操作公共模块")
    public DataResult<Object> getPublicModelData() {
        return new DataResult("操作公共模块成功！");
    }
}
